package com.wuyan.locationdog.ui.device

import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.arcsoft.commonbase.base.BaseBindingActivity
import com.arcsoft.commonbase.bus.Bus
import com.arcsoft.commonbase.bus.bus_device_info_modify
import com.arcsoft.commonbase.bus.bus_fence_refresh
import com.arcsoft.commonbase.ext.createEmptyView
import com.arcsoft.commonbase.ext.customShowLong
import com.arcsoft.commonbase.ext.gone
import com.arcsoft.commonbase.ext.visible
import com.arcsoft.commonbase.widgets.dialog.CommonDialog
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ToastUtils
import com.wuyan.locationdog.R
import com.wuyan.locationdog.adapter.FenceListAdapter
import com.wuyan.locationdog.databinding.DeviceFenceListActivityBinding
import com.wuyan.locationdog.help.DeviceHelp
import com.wuyan.locationdog.view.FenceAddPopup
import com.wuyan.locationdog.viewmodel.DeviceViewModel


class DeviceFenceListActivity : BaseBindingActivity<DeviceFenceListActivityBinding>() {
    private val mViewModel: DeviceViewModel by viewModels()

    private val mAdapter by lazy { FenceListAdapter(R.layout.item_device_fence, null) }

    override fun initData() {
        val textView = findViewById<TextView>(com.arcsoft.commonbase.R.id.tv_right_title)

        setTitleText("围栏管理", rightTitle = "管理") {
            binding.apply {
                if (textView.text.toString() == "管理") {
                    textView.text = "取消"
                    btnAdd.gone()
                    bottomLayout.visible()
                    mAdapter.showCheckBtn = true
                } else {
                    textView.text = "管理"
                    btnAdd.visible()
                    bottomLayout.gone()
                    mAdapter.showCheckBtn = false

                }
                mAdapter.notifyDataSetChanged()
            }
        }

        binding.apply {
            btnAdd.setOnClickListener {
                ActivityUtils.startActivity(FenceOvalActivity::class.java)

            }

            recyclerView.apply {
                layoutManager = LinearLayoutManager(context)
                adapter = mAdapter.apply {
                    setEmptyView(
                        createEmptyView(
                            "暂无围栏", com.arcsoft.commonbase.R.mipmap.empty_default,
                            "添加围栏后，将为您管理", "立即添加"
                        ) {
                            btnAdd.callOnClick()
                        }
                    )

                    setOnItemClickListener { adapter, view, position ->
                        if (textView.text.toString() == "取消") {
//                            data.find { it.isSelect }?.isSelect = false
//                            data[position].isSelect = true
                            data[position].isSelect = !data[position].isSelect
                            notifyItemChanged(position)
//                            notifyDataSetChanged()
                        }
                    }
                    addChildClickViewIds(R.id.btnEdit)
                    setOnItemChildClickListener { adapter, view, position ->

                        val bundle = Bundle()
                        bundle.putSerializable("FenceBeanContent", data[position])
                        ActivityUtils.startActivity(bundle, FenceOvalActivity::class.java)
                    }
                }
            }
            btnDelete.setOnClickListener {
                if (mAdapter.data.find { it.isSelect } == null) {
                    customShowLong("请选择删除的围栏")
                    return@setOnClickListener
                }
                CommonDialog(mActivity, title = "确定删除围栏吗？", "") {
                    val list = mAdapter.data.filter { it.isSelect }.map { it.areaId }
                    val map = HashMap<String, Any>()
                    map["areaIdList"] = list.toMutableList()
                    mViewModel.delArea(map)
                }.show()
            }

        }
        loadNetData()
    }

    override fun loadNetData() {
        super.loadNetData()
        val map = HashMap<String, Any>()
        mViewModel.areaList(map)
    }

    override fun observe() {
        super.observe()
        Bus.observe<String>(bus_fence_refresh, this) {
            loadNetData()
//            Bus.post(bus_device_info_modify, "")
        }
        mViewModel.fenceBean.observe(this) {
            mAdapter.setNewInstance(it.content.toMutableList())
            binding.apply {
                if (mAdapter.data.isNullOrEmpty()) {
                    findViewById<TextView>(com.arcsoft.commonbase.R.id.tv_right_title).apply {
                        gone()
                        text = "管理"
                        btnAdd.visible()
                        bottomLayout.gone()
                        mAdapter.showCheckBtn = false
                    }
                } else {
                    findViewById<TextView>(com.arcsoft.commonbase.R.id.tv_right_title).visible()
                    btnAdd.visible()
                }
            }


        }
        mViewModel.anyBean.observe(this) {
            customShowLong("删除成功")

//            loadNetData()
            Bus.post(bus_fence_refresh, "")
        }
    }
}